package com.example.datedemo2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.datedemo2.domain.WeekSample;
import com.example.datedemo2.mapper.WeekSampleMapper;
import com.example.datedemo2.service.dto.ResponseResult;
import com.example.datedemo2.service.dto.weekSample.WeekSampleAddDto;
import com.example.datedemo2.service.dto.weekSample.WeekSampleUpdateDto;
import com.example.datedemo2.service.tools.StaticTools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

import static com.example.datedemo2.service.tools.StaticTools.*;

@Service
public class WeekSampleService {
    @Resource
    private WeekSampleMapper weekSampleMapper;

    // 显示所有Week
    public List<WeekSample> selectAll() {
        return weekSampleMapper.selectList(null);
    }

    // 根据id显示Week
    public ResponseResult selectById(String id) {
        if (id == null || id == "") {
            return new ResponseResult(2, "null");
        }
        WeekSample entity = weekSampleMapper.selectById(id);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }
        return new ResponseResult(1, "成功", entity);
    }

    // 根据projectId,monthParameter,weekParameter显示week
    public ResponseResult selectByWeekParameter(int projectId, String monthParameter, int weekParameter) {
        if (weekParameter == 0 || monthParameter == null) {
            return new ResponseResult(2, "null");
        }

        QueryWrapper<WeekSample> weekSampleQueryWrapper = new QueryWrapper<>();
        try {
            weekSampleQueryWrapper.select().eq("week_number", transToWeekNumber(monthParameter, weekParameter)).eq("project_id", projectId);
        } catch (ParseException e) {
            return new ResponseResult(3, "日期格式错误，请使用类似“2015-02”格式");
        }

        List<WeekSample> entices = weekSampleMapper.selectList(weekSampleQueryWrapper);
        if (entices == null) {
            return new ResponseResult(2, "没有该对象");
        }
        return new ResponseResult(1, "成功", entices);
    }

    // 内部调用
    // 根据projectId,monthParameter显示范围内的week
    public List<WeekSample> selectByWeekRange(int projectId, String month) {

        QueryWrapper<WeekSample> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select().eq("project_id", projectId).apply("date_format(month_number,'%Y-%m') = {0}", monthNumber);

        if (month.length() < 6) {
            return null;
        }

        if (month.charAt(5) == '0') {
            String y = month.substring(0, 5);
            String m = month.substring(6, 7);
            queryWrapper.select().eq("project_id", projectId).apply("date_format(week_number,'%Y-%c') = {0}", y + m);

        } else {
            queryWrapper.select().eq("project_id", projectId).apply("date_format(week_number,'%Y-%c') = {0}", month);
        }

        return weekSampleMapper.selectList(queryWrapper);
    }

    // 新增Week
    public ResponseResult insertWeekSample(WeekSampleAddDto weekSampleAddDto) {
        WeekSample weekSample = new WeekSample();

        weekSample.setProjectId(weekSampleAddDto.getProjectId());
        try {
            if (StaticTools.isNormalWeek(weekSampleAddDto.getMonthParameter(), weekSampleAddDto.getWeekParameter())) {
                return new ResponseResult(0, "周数不正常");
            }
            weekSample.setWeekNumber(transToWeekNumber(weekSampleAddDto.getMonthParameter(), weekSampleAddDto.getWeekParameter()));
        } catch (ParseException e) {
            return new ResponseResult(3, "日期格式错误，请使用类似“2015-02”格式");
        }

        weekSample.setWeekText(weekSampleAddDto.getWeekText());

        // 唯一性判定，查询是否已存在
        QueryWrapper<WeekSample> weekSampleQueryWrapper = new QueryWrapper<>();
        try {
            weekSampleQueryWrapper.select().eq("week_number", transToWeekNumber(weekSampleAddDto.getMonthParameter(), weekSampleAddDto.getWeekParameter())).eq("project_id", weekSampleAddDto.getProjectId());
        } catch (ParseException e) {
            return new ResponseResult(3, "日期格式错误，请使用类似“2015-02”格式");
        }

        List<WeekSample> entices = weekSampleMapper.selectList(weekSampleQueryWrapper);
        if (entices.size() != 0) {
            return new ResponseResult(3, "该周数据已存在");
        }

        return getModifyResult(weekSampleMapper.insert(weekSample), weekSample);

//        try {
//            return getModifyResult(weekSampleMapper.insert(weekSample), weekSample);
//        } catch (DuplicateKeyException e) {
//            return new ResponseResult(3, "主键重复，该周数据可能已存在");
//        }
    }

    // 更新Week
    public ResponseResult updateWeekSample(WeekSampleUpdateDto weekSampleUpdateDto) {
        WeekSample weekSample = new WeekSample();

        weekSample.setProjectId(weekSampleUpdateDto.getProjectId());
        weekSample.setWeekId(weekSampleUpdateDto.getWeekId());
        try {
            if (StaticTools.isNormalWeek(weekSampleUpdateDto.getMonthParameter(), weekSampleUpdateDto.getWeekParameter())) {
                return new ResponseResult(0, "周数不正常");
            }
            weekSample.setWeekNumber(transToWeekNumber(weekSampleUpdateDto.getMonthParameter(), weekSampleUpdateDto.getWeekParameter()));
        } catch (ParseException e) {
            return new ResponseResult(3, "日期格式错误，请使用类似“2015-02”格式");
        }

        weekSample.setWeekText(weekSampleUpdateDto.getWeekText());

        return getModifyResult(weekSampleMapper.updateById(weekSample), weekSample);

//        try {
//            return getModifyResult(weekSampleMapper.updateById(weekSample), weekSample);
//        } catch (DuplicateKeyException e) {
//            return new ResponseResult(3, "该周数据已存在或主键重复");
//        }
    }

    // 根据id删除Week
    public ResponseResult deleteWeekSampleById(String id) {
        int deleteNum = weekSampleMapper.deleteById(id);
        if (deleteNum == 0) {
            return new ResponseResult(0, "删除失败 或 无此id对象");
        }
        return new ResponseResult(1, "成功");
    }

}
